package lo

import (
	"math/rand"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestIndexOf(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := IndexOf([]int{0, 1, 2, 1, 2, 3}, 2)
	result2 := IndexOf([]int{0, 1, 2, 1, 2, 3}, 6)

	is.Equal(2, result1)
	is.Equal(-1, result2)
}

func TestLastIndexOf(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := LastIndexOf([]int{0, 1, 2, 1, 2, 3}, 2)
	result2 := LastIndexOf([]int{0, 1, 2, 1, 2, 3}, 6)

	is.Equal(4, result1)
	is.Equal(-1, result2)
}

func TestHasPrefix(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	is.True(HasPrefix([]int{1, 2, 3, 4}, []int{1, 2}))
	is.False(HasPrefix([]int{1, 2, 3, 4}, []int{42}))
	is.True(HasPrefix([]int{1, 2, 3, 4}, nil))
}

func TestHasSuffix(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	is.True(HasSuffix([]int{1, 2, 3, 4}, []int{3, 4}))
	is.False(HasSuffix([]int{1, 2, 3, 4}, []int{42}))
	is.True(HasSuffix([]int{1, 2, 3, 4}, nil))
}

func TestFind(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	index := 0
	result1, ok1 := Find([]string{"a", "b", "c", "d"}, func(item string) bool {
		is.Equal([]string{"a", "b", "c", "d"}[index], item)
		index++
		return item == "b"
	})

	result2, ok2 := Find([]string{"foobar"}, func(item string) bool {
		is.Equal("foobar", item)
		return item == "b"
	})

	is.True(ok1)
	is.Equal("b", result1)
	is.False(ok2)
	is.Empty(result2)
}

func TestFindIndexOf(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	index := 0
	item1, index1, ok1 := FindIndexOf([]string{"a", "b", "c", "d", "b"}, func(item string) bool {
		is.Equal([]string{"a", "b", "c", "d", "b"}[index], item)
		index++
		return item == "b"
	})
	item2, index2, ok2 := FindIndexOf([]string{"foobar"}, func(item string) bool {
		is.Equal("foobar", item)
		return item == "b"
	})

	is.Equal("b", item1)
	is.True(ok1)
	is.Equal(1, index1)
	is.Empty(item2)
	is.False(ok2)
	is.Equal(-1, index2)
}

func TestFindLastIndexOf(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	index := 0
	item1, index1, ok1 := FindLastIndexOf([]string{"a", "b", "c", "d", "b"}, func(item string) bool {
		is.Equal([]string{"b", "d", "c", "b", "a"}[index], item)
		index++
		return item == "b"
	})
	item2, index2, ok2 := FindLastIndexOf([]string{"foobar"}, func(item string) bool {
		is.Equal("foobar", item)
		return item == "b"
	})

	is.Equal("b", item1)
	is.True(ok1)
	is.Equal(4, index1)
	is.Empty(item2)
	is.False(ok2)
	is.Equal(-1, index2)
}

func TestFindOrElse(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	index := 0
	result1 := FindOrElse([]string{"a", "b", "c", "d"}, "x", func(item string) bool {
		is.Equal([]string{"a", "b", "c", "d"}[index], item)
		index++
		return item == "b"
	})
	result2 := FindOrElse([]string{"foobar"}, "x", func(item string) bool {
		is.Equal("foobar", item)
		return item == "b"
	})

	is.Equal("b", result1)
	is.Equal("x", result2)
}

func TestFindKey(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1, ok1 := FindKey(map[string]int{"foo": 1, "bar": 2, "baz": 3}, 2)
	is.Equal("bar", result1)
	is.True(ok1)

	result2, ok2 := FindKey(map[string]int{"foo": 1, "bar": 2, "baz": 3}, 42)
	is.Empty(result2)
	is.False(ok2)

	type test struct {
		foobar string
	}

	result3, ok3 := FindKey(map[string]test{"foo": {"foo"}, "bar": {"bar"}, "baz": {"baz"}}, test{"foo"})
	is.Equal("foo", result3)
	is.True(ok3)

	result4, ok4 := FindKey(map[string]test{"foo": {"foo"}, "bar": {"bar"}, "baz": {"baz"}}, test{"hello world"})
	is.Empty(result4)
	is.False(ok4)
}

func TestFindKeyBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1, ok1 := FindKeyBy(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(k string, v int) bool {
		return k == "foo"
	})
	is.Equal("foo", result1)
	is.True(ok1)

	result2, ok2 := FindKeyBy(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(k string, v int) bool {
		return false
	})
	is.Empty(result2)
	is.False(ok2)
}

func TestFindUniques(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := FindUniques([]int{1, 2, 3})
	is.Equal([]int{1, 2, 3}, result1)

	result2 := FindUniques([]int{1, 2, 2, 3, 1, 2})
	is.Equal([]int{3}, result2)

	result3 := FindUniques([]int{1, 2, 2, 1})
	is.Empty(result3)

	result4 := FindUniques([]int{})
	is.Empty(result4)

	type myStrings []string
	allStrings := myStrings{"", "foo", "bar"}
	nonempty := FindUniques(allStrings)
	is.IsType(nonempty, allStrings, "type preserved")
}

func TestFindUniquesBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := FindUniquesBy([]int{0, 1, 2}, func(i int) int {
		return i % 3
	})
	is.Equal([]int{0, 1, 2}, result1)

	result2 := FindUniquesBy([]int{0, 1, 2, 3, 4}, func(i int) int {
		return i % 3
	})
	is.Equal([]int{2}, result2)

	result3 := FindUniquesBy([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
		return i % 3
	})
	is.Empty(result3)

	result4 := FindUniquesBy([]int{}, func(i int) int {
		return i % 3
	})
	is.Empty(result4)

	type myStrings []string
	allStrings := myStrings{"", "foo", "bar"}
	nonempty := FindUniquesBy(allStrings, func(i string) string {
		return i
	})
	is.IsType(nonempty, allStrings, "type preserved")
}

func TestFindDuplicates(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := FindDuplicates([]int{1, 2, 2, 1, 2, 3})
	is.Equal([]int{1, 2}, result1)

	result2 := FindDuplicates([]int{1, 2, 3})
	is.Empty(result2)

	result3 := FindDuplicates([]int{})
	is.Empty(result3)

	type myStrings []string
	allStrings := myStrings{"", "foo", "bar"}
	nonempty := FindDuplicates(allStrings)
	is.IsType(nonempty, allStrings, "type preserved")
}

func TestFindDuplicatesBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := FindDuplicatesBy([]int{3, 4, 5, 6, 7}, func(i int) int {
		return i % 3
	})
	is.Equal([]int{3, 4}, result1)

	result2 := FindDuplicatesBy([]int{0, 1, 2, 3, 4}, func(i int) int {
		return i % 5
	})
	is.Empty(result2)

	result3 := FindDuplicatesBy([]int{}, func(i int) int {
		return i % 3
	})
	is.Empty(result3)

	type myStrings []string
	allStrings := myStrings{"", "foo", "bar"}
	nonempty := FindDuplicatesBy(allStrings, func(i string) string {
		return i
	})
	is.IsType(nonempty, allStrings, "type preserved")
}

func TestMin(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := Min([]int{1, 2, 3})
	result2 := Min([]int{3, 2, 1})
	result3 := Min([]time.Duration{time.Second, time.Minute, time.Hour})
	result4 := Min([]int{})

	is.Equal(1, result1)
	is.Equal(1, result2)
	is.Equal(time.Second, result3)
	is.Zero(result4)
}

func TestMinIndex(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1, index1 := MinIndex([]int{1, 2, 3})
	result2, index2 := MinIndex([]int{3, 2, 1})
	result3, index3 := MinIndex([]time.Duration{time.Second, time.Minute, time.Hour})
	result4, index4 := MinIndex([]int{})

	is.Equal(1, result1)
	is.Zero(index1)

	is.Equal(1, result2)
	is.Equal(2, index2)

	is.Equal(time.Second, result3)
	is.Zero(index3)

	is.Zero(result4)
	is.Equal(-1, index4)
}

func TestMinBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := MinBy([]string{"s1", "string2", "s3"}, func(item, mIn string) bool {
		return len(item) < len(mIn)
	})
	result2 := MinBy([]string{"string1", "string2", "s3"}, func(item, mIn string) bool {
		return len(item) < len(mIn)
	})
	result3 := MinBy([]string{}, func(item, mIn string) bool {
		return len(item) < len(mIn)
	})

	is.Equal("s1", result1)
	is.Equal("s3", result2)
	is.Empty(result3)
}

func TestMinIndexBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1, index1 := MinIndexBy([]string{"s1", "string2", "s3"}, func(item, mIn string) bool {
		return len(item) < len(mIn)
	})
	result2, index2 := MinIndexBy([]string{"string1", "string2", "s3"}, func(item, mIn string) bool {
		return len(item) < len(mIn)
	})
	result3, index3 := MinIndexBy([]string{}, func(item, mIn string) bool {
		return len(item) < len(mIn)
	})

	is.Equal("s1", result1)
	is.Zero(index1)

	is.Equal("s3", result2)
	is.Equal(2, index2)

	is.Empty(result3)
	is.Equal(-1, index3)
}

func TestEarliest(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	a := time.Now()
	b := a.Add(time.Hour)
	result1 := Earliest(a, b)
	result2 := Earliest()

	is.Equal(a, result1)
	is.Zero(result2)
}

func TestEarliestBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	type foo struct {
		bar time.Time
	}

	t1 := time.Now()
	t2 := t1.Add(time.Hour)
	t3 := t1.Add(-time.Hour)
	result1 := EarliestBy([]foo{{t1}, {t2}, {t3}}, func(i foo) time.Time {
		return i.bar
	})
	result2 := EarliestBy([]foo{{t1}}, func(i foo) time.Time {
		return i.bar
	})
	result3 := EarliestBy([]foo{}, func(i foo) time.Time {
		return i.bar
	})

	is.Equal(foo{t3}, result1)
	is.Equal(foo{t1}, result2)
	is.Zero(result3)
}

func TestMax(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := Max([]int{1, 2, 3})
	result2 := Max([]int{3, 2, 1})
	result3 := Max([]time.Duration{time.Second, time.Minute, time.Hour})
	result4 := Max([]int{})

	is.Equal(3, result1)
	is.Equal(3, result2)
	is.Equal(time.Hour, result3)
	is.Zero(result4)
}

func TestMaxIndex(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1, index1 := MaxIndex([]int{1, 2, 3})
	result2, index2 := MaxIndex([]int{3, 2, 1})
	result3, index3 := MaxIndex([]time.Duration{time.Second, time.Minute, time.Hour})
	result4, index4 := MaxIndex([]int{})

	is.Equal(3, result1)
	is.Equal(2, index1)

	is.Equal(3, result2)
	is.Zero(index2)

	is.Equal(time.Hour, result3)
	is.Equal(2, index3)

	is.Zero(result4)
	is.Equal(-1, index4)
}

func TestMaxBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := MaxBy([]string{"s1", "string2", "s3"}, func(item, mAx string) bool {
		return len(item) > len(mAx)
	})
	result2 := MaxBy([]string{"string1", "string2", "s3"}, func(item, mAx string) bool {
		return len(item) > len(mAx)
	})
	result3 := MaxBy([]string{}, func(item, mAx string) bool {
		return len(item) > len(mAx)
	})

	is.Equal("string2", result1)
	is.Equal("string1", result2)
	is.Empty(result3)
}

func TestMaxIndexBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1, index1 := MaxIndexBy([]string{"s1", "string2", "s3"}, func(item, mAx string) bool {
		return len(item) > len(mAx)
	})
	result2, index2 := MaxIndexBy([]string{"string1", "string2", "s3"}, func(item, mAx string) bool {
		return len(item) > len(mAx)
	})
	result3, index3 := MaxIndexBy([]string{}, func(item, mAx string) bool {
		return len(item) > len(mAx)
	})

	is.Equal("string2", result1)
	is.Equal(1, index1)

	is.Equal("string1", result2)
	is.Zero(index2)

	is.Empty(result3)
	is.Equal(-1, index3)
}

func TestLatest(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	a := time.Now()
	b := a.Add(time.Hour)
	result1 := Latest(a, b)
	result2 := Latest()

	is.Equal(b, result1)
	is.Zero(result2)
}

func TestLatestBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	type foo struct {
		bar time.Time
	}

	t1 := time.Now()
	t2 := t1.Add(time.Hour)
	t3 := t1.Add(-time.Hour)
	result1 := LatestBy([]foo{{t1}, {t2}, {t3}}, func(i foo) time.Time {
		return i.bar
	})
	result2 := LatestBy([]foo{{t1}}, func(i foo) time.Time {
		return i.bar
	})
	result3 := LatestBy([]foo{}, func(i foo) time.Time {
		return i.bar
	})

	is.Equal(foo{t2}, result1)
	is.Equal(foo{t1}, result2)
	is.Zero(result3)
}

func TestFirst(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1, ok1 := First([]int{1, 2, 3})
	result2, ok2 := First([]int{})

	is.Equal(1, result1)
	is.True(ok1)
	is.Zero(result2)
	is.False(ok2)
}

func TestFirstOrEmpty(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := FirstOrEmpty([]int{1, 2, 3})
	result2 := FirstOrEmpty([]int{})
	result3 := FirstOrEmpty([]string{})

	is.Equal(1, result1)
	is.Zero(result2)
	is.Empty(result3)
}

func TestFirstOr(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := FirstOr([]int{1, 2, 3}, 63)
	result2 := FirstOr([]int{}, 23)
	result3 := FirstOr([]string{}, "test")

	is.Equal(1, result1)
	is.Equal(23, result2)
	is.Equal("test", result3)
}

func TestLast(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1, ok1 := Last([]int{1, 2, 3})
	result2, ok2 := Last([]int{})

	is.Equal(3, result1)
	is.True(ok1)
	is.Zero(result2)
	is.False(ok2)
}

func TestLastOrEmpty(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := LastOrEmpty([]int{1, 2, 3})
	result2 := LastOrEmpty([]int{})
	result3 := LastOrEmpty([]string{})

	is.Equal(3, result1)
	is.Zero(result2)
	is.Empty(result3)
}

func TestLastOr(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := LastOr([]int{1, 2, 3}, 63)
	result2 := LastOr([]int{}, 23)
	result3 := LastOr([]string{}, "test")

	is.Equal(3, result1)
	is.Equal(23, result2)
	is.Equal("test", result3)
}

func TestNth(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1, err1 := Nth([]int{0, 1, 2, 3}, 2)
	result2, err2 := Nth([]int{0, 1, 2, 3}, -2)
	result3, err3 := Nth([]int{0, 1, 2, 3}, 42)
	result4, err4 := Nth([]int{}, 0)
	result5, err5 := Nth([]int{42}, 0)
	result6, err6 := Nth([]int{42}, -1)

	is.Equal(2, result1)
	is.NoError(err1)
	is.Equal(2, result2)
	is.NoError(err2)
	is.Zero(result3)
	is.EqualError(err3, "nth: 42 out of slice bounds")
	is.Zero(result4)
	is.EqualError(err4, "nth: 0 out of slice bounds")
	is.Equal(42, result5)
	is.NoError(err5)
	is.Equal(42, result6)
	is.NoError(err6)
}

func TestNthOr(t *testing.T) {
	t.Parallel()

	t.Run("Integers", func(t *testing.T) {
		t.Parallel()
		is := assert.New(t)

		const defaultValue = -1
		intSlice := []int{10, 20, 30, 40, 50}

		is.Equal(30, NthOr(intSlice, 2, defaultValue))
		is.Equal(50, NthOr(intSlice, -1, defaultValue))
		is.Equal(defaultValue, NthOr(intSlice, 5, defaultValue))
	})

	t.Run("Strings", func(t *testing.T) {
		t.Parallel()
		is := assert.New(t)

		const defaultValue = "none"
		strSlice := []string{"apple", "banana", "cherry", "date"}

		is.Equal("banana", NthOr(strSlice, 1, defaultValue))      // Index 1, expected "banana"
		is.Equal("cherry", NthOr(strSlice, -2, defaultValue))     // Negative index -2, expected "cherry"
		is.Equal(defaultValue, NthOr(strSlice, 10, defaultValue)) // Out of bounds, fallback "none"
	})

	t.Run("Structs", func(t *testing.T) {
		t.Parallel()
		is := assert.New(t)

		type User struct {
			ID   int
			Name string
		}
		userSlice := []User{
			{ID: 1, Name: "Alice"},
			{ID: 2, Name: "Bob"},
			{ID: 3, Name: "Charlie"},
		}

		expectedUser := User{ID: 1, Name: "Alice"}
		is.Equal(expectedUser, NthOr(userSlice, 0, User{ID: 0, Name: "Unknown"}))

		expectedUser = User{ID: 3, Name: "Charlie"}
		is.Equal(expectedUser, NthOr(userSlice, -1, User{ID: 0, Name: "Unknown"}))

		expectedUser = User{ID: 0, Name: "Unknown"}
		is.Equal(expectedUser, NthOr(userSlice, 10, User{ID: 0, Name: "Unknown"}))
	})
}

func TestNthOrEmpty(t *testing.T) {
	t.Parallel()

	t.Run("Integers", func(t *testing.T) {
		t.Parallel()
		is := assert.New(t)

		intSlice := []int{10, 20, 30, 40, 50}

		is.Equal(30, NthOrEmpty(intSlice, 2))
		is.Equal(50, NthOrEmpty(intSlice, -1))
		is.Zero(NthOrEmpty(intSlice, 10))
	})

	t.Run("Strings", func(t *testing.T) {
		t.Parallel()
		is := assert.New(t)

		strSlice := []string{"apple", "banana", "cherry", "date"}

		is.Equal("banana", NthOrEmpty(strSlice, 1))
		is.Equal("cherry", NthOrEmpty(strSlice, -2))
		is.Empty(NthOrEmpty(strSlice, 10))
	})

	t.Run("Structs", func(t *testing.T) {
		t.Parallel()
		is := assert.New(t)

		type User struct {
			ID   int
			Name string
		}
		userSlice := []User{
			{ID: 1, Name: "Alice"},
			{ID: 2, Name: "Bob"},
			{ID: 3, Name: "Charlie"},
		}

		expectedUser := User{ID: 1, Name: "Alice"}
		is.Equal(expectedUser, NthOrEmpty(userSlice, 0))

		expectedUser = User{ID: 3, Name: "Charlie"}
		is.Equal(expectedUser, NthOrEmpty(userSlice, -1))

		is.Zero(NthOrEmpty(userSlice, 10))
	})
}

func TestSample(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := Sample([]string{"a", "b", "c"})
	result2 := Sample([]string{})

	is.True(Contains([]string{"a", "b", "c"}, result1))
	is.Empty(result2)
}

func TestSampleBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	r := rand.New(rand.NewSource(42))

	result1 := SampleBy([]string{"a", "b", "c"}, r.Intn)
	result2 := SampleBy([]string{}, rand.Intn)

	is.True(Contains([]string{"a", "b", "c"}, result1))
	is.Empty(result2)
}

func TestSamples(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := Samples([]string{"a", "b", "c"}, 3)
	result2 := Samples([]string{}, 3)

	is.ElementsMatch(result1, []string{"a", "b", "c"})
	is.Empty(result2)

	type myStrings []string
	allStrings := myStrings{"", "foo", "bar"}
	nonempty := Samples(allStrings, 2)
	is.IsType(nonempty, allStrings, "type preserved")
}

func TestSamplesBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	r := rand.New(rand.NewSource(42))

	result1 := SamplesBy([]string{"a", "b", "c"}, 3, r.Intn)
	result2 := SamplesBy([]string{}, 3, r.Intn)

	is.ElementsMatch(result1, []string{"a", "b", "c"})
	is.Empty(result2)

	type myStrings []string
	allStrings := myStrings{"", "foo", "bar"}
	nonempty := SamplesBy(allStrings, 2, r.Intn)
	is.IsType(nonempty, allStrings, "type preserved")
}
